
*===============================================================================
* Title: Two Levels - Vote Choice
* Author: Agatha, Alex
*===============================================================================


*** Vote choice federal election (übernommen von Maria)

label define wabsfull ///
	1 "CDU" ///
	4 "SPD" ///
	5 "FDP" ///
	6 "Greens" ///
	7 "The Left" ///
	322 "AfD" ///
	801 "other party" ///
	8888 "don't know" ///
	9999 "won't vote"

*** Vote intention pre-election
foreach x of numlist $allpre 9 {
	generate wabsfull`x' = kp`x'_190bb
	replace wabsfull`x' = 8888 if kp`x'_190bb ==.b // "weiß nicht"
	replace wabsfull`x' = . if inlist(kp`x'_190bb,.a,.c,.e,.f,.g,.l,.h,808) //missings & "sonstige Nennung"
	replace wabsfull`x' = 801 if kp`x'_190bb>7 & kp`x'_190bb<=801 & kp`x'_190bb!=322  // "andere Partei" außer AfD
	replace wabsfull`x' = 9999 if inlist(kp`x'_170,4,5) //"Nichtwahl
	label value wabsfull`x' wabsfull
}


***  vote choice post-election
foreach x of numlist $post {
	generate wabsfull`x' = kp`x'_200bb
	replace wabsfull`x' = . if kp`x'_200bb > 801 //alle missings gleicher Code
	replace wabsfull`x' = 801 if kp`x'_200bb>7 & kp`x'_200bb<=801 & kp`x'_200bb!=322 // alle anderen Parteien außer AfD auf 801 setzen
	replace wabsfull`x' = 9999 if kp`x'_180 == 2 // Nichtwähler auf 9999 setzen
	label value wabsfull`x' wabsfull // Wertelabel
}

order wabsfull9, after(wabsfull8)

*** Include postal voters
foreach welle of numlist $brief {		
	if `welle'!=8 {
	replace wabsfull`welle' = kp`welle'_191bb if kp`welle'_170==6
	}
	else if `welle'==8 {
	replace wabsfull`welle' = kp`welle'_191bb if kp`welle'_180==6
	}
}



foreach x of numlist $briefop {            //wave in which postal voting is reported
    local y = `x'+1                                   //first wave after postal voting has been reported

            foreach z of numlist `y'/8 {    //loop over all waves after postal vote has been reprted until post-election wave
                replace wabsfull`z' = wabsfull`x' if kp`x'_170==6
        }
} 




foreach welle of numlist $allpre 8 {
	replace wabsfull`welle' = 801 if wabsfull`welle'>7 & wabsfull`welle'!=322 & wabsfull`welle'<801
	recode wabsfull`welle' (.a .c .e .g=.)
}


//Dynamische Bundeswahlabsicht auch nach Briefwahl

foreach y of numlist $all {
	clonevar wabsfull_hyp`y'  = wabsfull`y'  
	lab var wabsfull_hyp`y' "(Hyp.) Wahlabsicht Bund W`welle'"
}

	foreach welle of numlist $briefop {            //wave in which postal voting is reported
	local y = `welle'+1    

                                   //first wave after postal voting has been reported
	replace wabsfull_hyp`y' = kp`y'_2770ba if kp`welle'_170==6
     
  }



*** Generate Dummy-Variables, national level (incl. non-voters & don't know)
foreach x of numlist $all {
	gen B_votecdu`x' = 1 if wabsfull`x'==1
	gen B_votespd`x' = 1 if wabsfull`x'==4
	gen B_votefdp`x' = 1 if wabsfull`x'==5		
	gen B_votegru`x' = 1 if wabsfull`x'==6
	gen B_votelin`x' = 1 if wabsfull`x'==7
	gen B_voteafd`x' = 1 if wabsfull`x'==322
	
	foreach part in cdu spd fdp gru lin afd {
		recode B_vote`part'`x' (.=0) if wabsfull`x'!=.
	}
}
	

*** Vote choice regional election


* Vote intention pre-election
foreach x of numlist $allpre $post {
	gen NIwahl`x' = kp`x'_3197b
	replace NIwahl`x' = 8888 if kp`x'_3197b ==.b // "weiß nicht"
	replace NIwahl`x' = 9999 if inlist(kp`x'_3187,4,5) //"Nichtwahl"
	recode NIwahl`x' (.a .c .e .g=.)
	label value NIwahl`x' wabsfull
	recode NIwahl`x' (2=1)
}


*  vote choice post-election
gen NIwahl9 = kp9_3198b
replace NIwahl9 = 9999 if kp9_3188 == 2 // Nichtwähler auf 9999 setzen
recode NIwahl9 (.a .c .e .g .k=.)
label value NIwahl9 wabsfull // Wertelabel
recode NIwahl9 (2=1) 

fre NIwahl5


*** Generate Dummy-Variables, regional Level (incl. non-voters & don't know)
foreach x of numlist $all {
	gen L_votecdu`x' = 1 if NIwahl`x'==1
	gen L_votespd`x' = 1 if NIwahl`x'==4
	gen L_votefdp`x' = 1 if NIwahl`x'==5		
	gen L_votegru`x' = 1 if NIwahl`x'==6
	gen L_votelin`x' = 1 if NIwahl`x'==7
	gen L_voteafd`x' = 1 if NIwahl`x'==322
	
	foreach part in cdu spd fdp gru lin afd {
		recode L_vote`part'`x' (.=0) if NIwahl`x'!=.
	}
}




*** Dummy: same party choice for federal and regional election (incl. don't know
*** & non-voters)
foreach x of numlist $all {
gen same_hyp`x' = 0 if !missing(NIwahl`x') & !missing(wabsfull_hyp`x')
replace same_hyp`x' = 1 if wabsfull_hyp`x'== NIwahl`x' & !missing(NIwahl`x') & !missing(wabsfull_hyp`x')
lab var same_hyp`x' "same vote choice national & regional level, wave `x'"
}

*Including dynamic changes after postal voting
foreach x of numlist $all {
gen same`x' = 0 if !missing(NIwahl`x') & !missing(wabsfull`x')
replace same`x' = 1 if wabsfull`x'== NIwahl`x' & !missing(NIwahl`x') & !missing(wabsfull`x')
lab var same`x' "same vote choice national & regional level, wave `x'"
}

* same party in final choice (incl. don't know & non-voters)
gen samefvc = 0 if !missing(NIwahl9) & !missing(wabsfull8)
replace samefvc = 1 if wabsfull8== NIwahl9 & !missing(NIwahl9) & !missing(wabsfull8)
lab var samefvc "same final vote choice in national & regional election"



